package club.adger.controller.home;

import club.adger.service.LoginService;
import club.adger.utils.CpachaUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: Adger
 * @Date: 2020/07/03/5:11
 */
@Controller
@RequestMapping("/login")
public class LoginController {
    /**
     注入LoginService
     */
    @Autowired
    private LoginService loginService;

    /**
     * 后台登录地址
     * @return
     */
    @GetMapping("/loginhome")
    public String loginhome (){
        return "admin/login";
    }

    /**
     * 用户主页 数据展示页面
     * @return
     */
    @GetMapping("/userhome")
    public String home (){
        return "list";
    }

    /**
     * 登录验证用户名存在 跟 不存在 密码对不对 的方法 服务层 成功的话设置了session
     * @param request
     * @return
     */
    @PostMapping("/userlogin")
    @ResponseBody
    public JSONObject login(HttpServletRequest request){
        // 拿到验证码
        String loginCpacha = (String) request.getSession().getAttribute("loginCpacha");
        JSONObject result = new JSONObject();
        String sCpacha = request.getParameter("cpacha");
        System.out.println(sCpacha);
        // 验证码判断 toUpperCase() 方法用于把字符串转换为大写。
        if(sCpacha.toUpperCase().equals(loginCpacha.toUpperCase())){
            //返回值对象
            result = loginService.login(request);
            return result;
        }else {
            result.put("message","验证码错误");
            //判断是否登录成功表示符
            result.put("success",false);
            return result;
        }
    }

    /**
     * 退出登录操作
     * @param session
     * @return
     */
    @GetMapping("/logout")
    public String logout (HttpSession session){
        session.invalidate();
        return "admin/login";
    }

    /**
     * 验证码
     * @param vcodeLen
     * @param width
     * @param height
     * @param cpachaType:用来区别验证码的类型，传入字符串
     */
    @GetMapping("/get_cpacha")
    public void generateCpacha(
            @RequestParam(name="vl",required=false,defaultValue="4") Integer vcodeLen,
            @RequestParam(name="w",required=false,defaultValue="100") Integer width,
            @RequestParam(name="h",required=false,defaultValue="30") Integer height,
            @RequestParam(name="type",required=true,defaultValue="loginCpacha") String cpachaType,
            HttpServletRequest request,
            HttpServletResponse response){
        CpachaUtil cpachaUtil = new CpachaUtil(vcodeLen, width, height);
        String generatorVCode = cpachaUtil.generatorVCode();
        //获取baisession对象,然后把要du绑定对象/值 帮定值到session对象上 用户的一次会话共享一个session对象
        request.getSession().setAttribute(cpachaType, generatorVCode);
        BufferedImage generatorRotateVCodeImage = cpachaUtil.generatorRotateVCodeImage(generatorVCode, true);
        try {
            ImageIO.write(generatorRotateVCodeImage, "gif", response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
